LogFile = None
LogLevel = 3 # 0 - error, 1 - warning, 2 - info, 3 - debug
LogDuplicateToStdout = False
from time import time
from datetime import datetime


def logInit(LogFName, LogLVL, DuplicateToStdout):
    global LogFile
    global LogLevel
    global LogDuplicateToStdout
    try:
        LogFile = open(LogFName, 'a')
        LogLevel = LogLVL
        LogDuplicateToStdout = DuplicateToStdout
    except (Exception), err:
        print 'Error: unable to open log file(%s), please check path! Exception: %s' % (LogFName, str(err))

def logMsg(Prefix, Message):
    global LogFile
    global LogDuplicateToStdout
    try:
        LogFile.write('%s %s: %s\n' % (str(datetime.fromtimestamp(time())), Prefix, Message))
        LogFile.flush()
        if LogDuplicateToStdout == True:
            print('%s %s: %s\n' % (str(datetime.fromtimestamp(time())), Prefix, Message))
    except (Exception), err:
        print 'Error: %s Message: %s' % (str(err), Prefix + ':' + Message)
        
def logError(Message):
    logMsg('Error', Message)

def logWarn(Message):
    global LogLevel
    if LogLevel >= 1:
        logMsg('Warning', Message)

def logInfo(Message):
    global LogLevel
    if LogLevel >= 2:
        logMsg('Info', Message)

def logDebug(Message):
    global LogLevel
    if LogLevel >= 3:
        logMsg('Debug', Message)

